package com.xyf.blog.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ServletContext context = request.getServletContext();
        Object user=context.getAttribute("user");
        if (user!=null){
//            放行
            return true;
        }
           request.setAttribute("msg","请先登录");
//           请求转发，保留了request中的信息，只有一个request
           request.getRequestDispatcher("/admin").forward(request,response);
           return false;

    }
}
